package com.example.mydy;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.mydy.model.UserData;
import com.example.mydy.model.UserInfo;
import com.example.mydy.utils.MockDataManager;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class DownlineUsersFragment extends Fragment {
    private static final String ARG_LEVEL = "level";
    private int level;
    private RecyclerView recyclerView;
    private LinearLayout emptyView;
    private DownlineUserAdapter adapter;

    public static DownlineUsersFragment newInstance(int level) {
        DownlineUsersFragment fragment = new DownlineUsersFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_LEVEL, level);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            level = getArguments().getInt(ARG_LEVEL);
        }
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_downline_users, container, false);
        recyclerView = view.findViewById(R.id.recycler_view);
        emptyView = view.findViewById(R.id.empty_view);
        
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        
        loadDownlineUsers();
        
        return view;
    }

    private void loadDownlineUsers() {
        // 获取当前用户名，使用getInstance()获取实例再调用方法
        String currentUsername = MockDataManager.getInstance().getCurrentUsername();
        
        if (currentUsername != null) {
            // 获取指定级别的下线用户详情
            Map<String, Double> downlineUsers = MockDataManager.getDownlineUsersDetail(currentUsername, level);
            
            if (downlineUsers.isEmpty()) {
                recyclerView.setVisibility(View.GONE);
                emptyView.setVisibility(View.VISIBLE);
            } else {
                recyclerView.setVisibility(View.VISIBLE);
                emptyView.setVisibility(View.GONE);
                
                // 转换为用户列表
                List<String> usernames = new ArrayList<>(downlineUsers.keySet());
                adapter = new DownlineUserAdapter(usernames, downlineUsers);
                recyclerView.setAdapter(adapter);
            }
        }
    }

    private class DownlineUserAdapter extends RecyclerView.Adapter<DownlineUserAdapter.ViewHolder> {
        private List<String> usernames;
        private Map<String, Double> contributions;

        public DownlineUserAdapter(List<String> usernames, Map<String, Double> contributions) {
            this.usernames = usernames;
            this.contributions = contributions;
        }

        @NonNull
        @Override
        public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_downline_user, parent, false);
            return new ViewHolder(view);
        }

        @Override
        public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
            String username = usernames.get(position);
            UserInfo userData = MockDataManager.findUserByUsername(username);
            
            if (userData != null) {
                holder.tvUsername.setText(userData.getNickname() != null ? userData.getNickname() : username);
                
                // 显示贡献值
                double contribution = contributions.get(username);
                holder.tvContribution.setText(String.format("贡献: ¥%.2f", contribution));
                
                // 加载头像
                String avatarUrl = MockDataManager.getInstance().getUserAvatarUrl(username);
                if (avatarUrl != null && !avatarUrl.isEmpty()) {
                    Picasso.get().load(avatarUrl).placeholder(R.drawable.ic_avatar_placeholder).into(holder.ivAvatar);
                } else {
                    holder.ivAvatar.setImageResource(R.drawable.ic_avatar_placeholder);
                }
            } else {
                holder.tvUsername.setText(username);
                holder.tvContribution.setText("贡献: ¥0.00");
                holder.ivAvatar.setImageResource(R.drawable.ic_avatar_placeholder);
            }
        }

        @Override
        public int getItemCount() {
            return usernames.size();
        }

        public class ViewHolder extends RecyclerView.ViewHolder {
            ImageView ivAvatar;
            TextView tvUsername;
            TextView tvContribution;

            public ViewHolder(@NonNull View itemView) {
                super(itemView);
                ivAvatar = itemView.findViewById(R.id.iv_avatar);
                tvUsername = itemView.findViewById(R.id.tv_username);
                tvContribution = itemView.findViewById(R.id.tv_contribution);
            }
        }
    }
} 